/*
Author: Jason Bareswilt
Functionality: the GUI that displays the highscores.
Version number: 0.0.9
Testers: needs to be tested
Date written: November 22nd 2011
Date tested: needs to be tested
*/
package disease;

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.*;
import disease.RottingFlesh;

public class HighScores
{
	public static int count = 0;
	
	static void HighScoresGUI()
	{
		System.out.println("High Scores");
		RottingFlesh.frame.getContentPane().removeAll();
		DrawingArea drawingArea = new DrawingArea();
		ButtonPanel buttonPanel = new ButtonPanel( drawingArea );
		DrawingArea panel = new DrawingArea(new ImageIcon("ZombiesRevBHighScore.jpg").getImage());
		RottingFlesh.frame.getContentPane().add(panel);
		RottingFlesh.frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		RottingFlesh.frame.setSize(450, 450);
		RottingFlesh.frame.setResizable(false);
		RottingFlesh.frame.setVisible(true);
	}

	static class ButtonPanel extends JPanel implements ActionListener
	{
		private static final long serialVersionUID = 1L;

		public ButtonPanel(DrawingArea drawingArea)
		{
			add( createButton("Main Menu", Color.RED) );
			add( createButton("Reset Scores", Color.RED) );
			add( createButton("Quit", Color.RED) );
			setBackground(Color.BLACK);
		}

		private JButton createButton(String text, Color background)
		{
			JButton button = new JButton(text);
			button.setBackground(background);
			button.addActionListener(this);
			UIManager.put("Button.select", Color.red);
			updateUI();
			return button;
		}

		public void actionPerformed(ActionEvent e)
		{
			//JButton button = (JButton)e.getSource();
			
			if ("Main Menu".equals(e.getActionCommand()))
			{
				RottingFlesh.createAndShowGUI();
			}
			if ("Quit".equals(e.getActionCommand()))
			{
				System.exit(0);
			}
			if("Reset Scores".equals(e.getActionCommand()))
			{
				HighScorelog.resetScores();
				RottingFlesh.frame.repaint(); 
			}
		}
	}

	static class DrawingArea extends JPanel
	{
		private static final long serialVersionUID = 1L;
		private static Image img;
		
		public DrawingArea(String filename)
		{
			this(new ImageIcon(img).getImage());
		}
		
		public DrawingArea(Image img)
		{
			DrawingArea.img = img;
			Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);
			setLayout(null);
	    }
		
		public DrawingArea()
		{}
		
		public void paintComponent(Graphics g)
		{
			String[] ScoresNames = {"COMPUTER","COMPUTER","COMPUTER","COMPUTER","COMPUTER","COMPUTER","COMPUTER","COMPUTER","COMPUTER","COMPUTER"};
			String[] ScoresList = {"0","0","0","0","0","0","0","0","0","0"};
			Font font = new Font("Arial", Font.BOLD, 14);
			Graphics2D g2 = (Graphics2D) g;
			g2.drawImage(img, 0, 0, null);
			Color transBlack = new Color(0,0,0,175);
			g.setColor(transBlack);
			g.fillRect(100, 100, 260, 255);
			g2.setColor(Color.red);
			parseText(ScoresNames, ScoresList);
			g2.setFont(font);
			for (int i = 0; i < ScoresNames.length; i++)
			{
				if (i != 9)
					g2.drawString(i + 1 + ")", 120, 26*i + 115);
				if (i == 9)
					g2.drawString(i + 1 + ")", 112, 26*i + 115);
				g2.drawString(ScoresNames[i], 135, 26*i + 115);
				g2.drawString(ScoresList[i], 270, 26*i + 115);
			}
		}
		
		public void parseText(String[] ScoresNames, String[] ScoresList)
		{
			int index = 0;
			try
			{
				FileInputStream fs = new FileInputStream("Scores.csv");
				DataInputStream in = new DataInputStream(fs);
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String line;
					
				while ((line = br.readLine()) != null)
				{
					String lineSplit[] = line.split(",");
					ScoresNames[index] = lineSplit[1];
					ScoresList[index] = lineSplit[0];
					index++;
				}		
				fs.close();
				in.close();
				br.close();
				System.out.println("Read from document");
			}
			catch (Exception e)
			{
				System.err.println("Error reading from file: " + e.getMessage());
			}
		}
	}
}